package com.leetcode.offer.chapter2;

/**
 * @author Dennis Li
 * @date 2020/7/12 15:53
 */
public class RobotMovement_13 {

    private int sum = 0;
    private boolean[][] matrix;

    public int movingCount(int m, int n, int k) {
        matrix = new boolean[m][n];
        dfs(0, 0, k);
        return sum;
    }

    public boolean dfs(int a, int b, int k) {
        if (a < 0 || a >= matrix.length || b < 0 || b >= matrix[0].length
                || judgeIndex(a, b) > k || matrix[a][b]) return false;
        matrix[a][b] = true;
        boolean res = dfs(a + 1, b, k) || dfs(a - 1, b, k)
                || dfs(a, b + 1, k) || dfs(a, b - 1, k);
        if (res) sum++;
        return res;
    }

    public static int judgeIndex(int a, int b) {
        //String num = "" + a + b; -- 避免字符串操作
        int sum = 0;
        while (a > 0) {
            sum += a % 10;
            a /= 10;
        }
        while (b > 0) {
            sum += b % 10;
            b /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(judgeIndex(35, 37));
    }

}
